package com.tjw.telecomver2.service.impl;

import com.tjw.telecomver2.ex.CoolSharkServiceException;
import com.tjw.telecomver2.mapper.MenuPhoneMapper;
import com.tjw.telecomver2.mapper.PhoneMapper;
import com.tjw.telecomver2.mapper.SetMenuMapper;
import com.tjw.telecomver2.pojo.dto.MenuPhoneAddNewDTO;
import com.tjw.telecomver2.pojo.entity.CardHolder;
import com.tjw.telecomver2.pojo.entity.PhoneNumber;
import com.tjw.telecomver2.pojo.entity.SetMenu;
import com.tjw.telecomver2.service.ISetMenuService;
import com.tjw.telecomver2.web.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SetMenuServiceImpl implements ISetMenuService {

    @Autowired
    private MenuPhoneMapper menuPhoneMapper;

    @Autowired
    private PhoneMapper phoneMapper;

    @Autowired
    private SetMenuMapper setMenuMapper;

    /**
     * 新增套餐信息
     *
     * @param setMenu
     */
    @Override
    public void addNew(SetMenu setMenu) {
        log.debug("开始处理【新增套餐信息】业务，参数：", setMenu);
        // 判断套餐内的用户数量是否大于9个
        List<CardHolder> cardHolders = setMenu.getCardHolders();
        if (cardHolders.size() > 9) {
            // 是，抛出异常
            throw new CoolSharkServiceException(ResponseCode.ERR_CONFLICT, "一个套餐最多包含9张电话卡");
        }
        List<PhoneNumber> phoneNumberList = new ArrayList<>();
        // 否，拿到cardHolders办卡人信息
        for (CardHolder cardHolder : cardHolders) {
            // 拿到办卡人名字
            String name = cardHolder.getName();
            // 拿到phoneNumbers办卡人名下的电话卡信息
            List<PhoneNumber> phoneNumbers = cardHolder.getPhoneNumbers();
            // 判断该人名下电话卡是否超过5张
            if (phoneNumbers.size() > 5) {
                // 是，抛出异常
                throw new CoolSharkServiceException(ResponseCode.ERR_CONFLICT, "一个人名下最多包含5张电话卡");
            }
            // 否，获取电话号码信息
            for (PhoneNumber phoneNumber : phoneNumbers) {
                Long phone = phoneNumber.getPhone();
                Integer isMainCard = phoneNumber.getIsMainCard();
                Long phoneNumberId = phoneNumber.getId();
                log.debug("phoneNumberId: ", phoneNumberId);
                PhoneNumber pn = new PhoneNumber();
                pn.setPhone(phone);
                pn.setIsMainCard(isMainCard);
                pn.setName(name);
                phoneNumberList.add(pn);
            }
        }
        // 进行数据库操作
        // 1. 向set_menu中插入数据
        setMenuMapper.addNew(setMenu);
        // 2. 向phone中插入数据，目前有phone，name，is_main_card字段，暂时不考虑吧menu_phone_id  // todo 考虑删除该字段
        phoneMapper.addNewBatch(phoneNumberList);
        // 3. 向menu_phone表中插入数据
        MenuPhoneAddNewDTO menuPhoneAddNewDTO = new MenuPhoneAddNewDTO();

//        menuPhoneAddNewDTO.setPhoneId();
        Long setMenuId = setMenu.getId();
        log.debug("setMenuId: ", setMenuId);
        menuPhoneAddNewDTO.setSetMenuId(setMenuId);
        menuPhoneMapper.addNew(menuPhoneAddNewDTO);
        // todo   set_menu表中main_card_num和Cart_holder字段可以先不插入，查询的时候关联查询即可，考虑删除这两个字段
        // todo   set_menu表中要添加可加副卡数balance字段
        // todo   前端动态增减表单项

    }
}
